package com.oyc.demo.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.oyc.demo.domain.User;
import com.oyc.demo.service.UserService;
import com.oyc.demo.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName CommonController
 * @Description 公共配置类
 * @Author oyc
 * @Date 2020/12/2 9:36
 * @Version
 */
@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/findLoginUser",method=RequestMethod.POST)
    @ApiOperation("登录接口")
    public Result<String> findLoginUser(@RequestBody User user,HttpServletRequest request) throws IOException {
    	  if(user.getPassword()!=null&&user.getPassword().trim().length()>0){
    		  user.setPassword(user.getPassword().trim());
    	  }
    	  
          User bean=userService.findLoginUser(user);
          
          
        if(bean!=null&&bean.getProvincename().trim().length()>0){
        	
        	HttpSession session=request.getSession();
        	session.setAttribute("provincename", bean.getProvincename());
        	
        	userService.updatereset(null);
        	userService.updateloginflag(user);
        	//判定用户权限,1后端 0前端
        	if(bean.getProvincename().equals("物资公司")){
        		return Result.success("statisticsList");
        	}
        	else{
        		return Result.success("configureList");
        	}
        }
        return Result.error("用户名或者密码不正确",-1);
    }
    
    
    @RequestMapping(value="/resetpassword",method=RequestMethod.POST)
    @ApiOperation("修改密码接口")
    public Result<String> resetpassword(@RequestBody User user) throws IOException {
		     if((user.getNewPassword() == null || user.getNewPassword().trim().length()==0) ||
		        (user.getConfirmPassword() == null || user.getConfirmPassword().trim().length() == 0) ||
		        !user.getNewPassword().equals(user.getConfirmPassword())){
		     return Result.success("输入的密码不一致,请重新输入");
		  }
		
		     if(user.getOriginalPassword() == null || user.getOriginalPassword().trim().length()==0){
		        return Result.success("请输入旧密码");
		  }
		     if(user.getOriginalPassword().equals(user.getNewPassword())){
		         return Result.success("新密码与旧密码相同，请重新输入");
		   }
		
		     if("0".equals(user.getFlag())){
		     //物资公司
		     user.setProvincename("物资公司");
		  }else if ("1".equals(user.getFlag())){
		        //省公司
		     user.setLoginflag(1);
		  }else {
		        return Result.success("请确认你的角色");
		  }
		  user.setPassword(user.getOriginalPassword());
		  User bean=userService.findLoginUser(user);
		  if(bean == null){
		     return Result.success("输入的旧密码不正确");
		  }
		  user.setProvincename(bean.getProvincename());
		  user.setPassword(user.getNewPassword().trim());
		     userService.resetpassword(user);
		     return Result.success("修改密码成功");
		    }
}